/**
 * @file usr_hrpwm_handle.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-07-13
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#ifndef USR_HRPWM_HANDLE__H_H
#define USR_HRPWM_HANDLE__H_H

#ifdef __cplusplus
extern "C"{
#endif
#include<stdint.h>
#include<stdio.h>
#include "hc32_ll.h"
#include "ev_hc32f334_lqfp64_bsp.h"

/* unlock/lock peripheral */
#define EXAMPLE_PERIPH_WE               (LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU)
#define EXAMPLE_PERIPH_WP               (LL_PERIPH_EFM | LL_PERIPH_FCG)

/*****************For Buck*************** */
#define HRPWM_BUCK_PWM_UNIT_ID   (HRPWM_UNIT2)
#define HRPWM_BUCK_PWM_UNIT      (CM_HRPWM2)
#define HRPWM_BUCK_PWM_SYNC_UNIT (HRPWM_SW_SYNC_UNIT2)
#define HRPWM_BUCK_PWM_FCG       (FCG2_PERIPH_HRPWM_2)

#define HRPWM_BUCK_PORT              (GPIO_HRPWM2_PWMA | GPIO_HRPWM2_PWMB)
#define HRPWM_BUCK_CNT_PEAK_INT_IRQn (HRPWM_2_OVF_UDF_IRQn)
#define HRPWM_BUCK_BUCK_UNIT         (CM_HRPWM2)
/*********************************************************************************************** */

/****************For Boost*************** */
#define HRPWM_BST_PWM_UNIT_ID   (HRPWM_UNIT3)
#define HRPWM_BST_PWM_UNIT                  (CM_HRPWM3)
#define HRPWM_BST_PWM_SYNC_UNIT             (HRPWM_SW_SYNC_UNIT3)
#define HRPWM_BST_PWM_FCG                   (FCG2_PERIPH_HRPWM_3)
#define HRPWM_BST_PORT                      (GPIO_HRPWM3_PWMA | GPIO_HRPWM3_PWMB)
#define HRPWM_BST_CNT_PEAK_INT_IRQn         (HRPWM_3_OVF_UDF_IRQn)
/********************************************************************************************** */
#if 0
#define GET_HRPWM_PERIOD_VALUE(f0)    (100000000000lu/(26*f0)) /*HRPWM_MD_TRIANGLE*/
#else
#define GET_HRPWM_PERIOD_VALUE(f0)    (100000000lu/(13*f0))/*HRPWM_MD_SAWTOOTH*/
#endif 

#define HRPWM_DEFAULT_FREQ  (200u/*kHz*/)
/*F0=300kHz; Period value 100000000000/13*F0 = 12820*2 */
#define HRPWM_PERIOD_VALUE              ((unsigned int)GET_HRPWM_PERIOD_VALUE(HRPWM_DEFAULT_FREQ/*开关频率(kHz)*/))


// #define ADC_SAMPLE_PERIOD   (1.0f/(HRPWM_DEFAULT_FREQ*1000))    
#define ADC_SAMPLE_PERIOD   (0.000007f)  /*7uS*/

#define GET_CMP_VALUE_BY_DUTY_C(Duty)   (((HRPWM_PERIOD_VALUE*Duty)))
/* Compare value */
#define BOOST_MAX_DUTY_CYCLE  (0.95f)
#define BOOST_MINI_DUTY_CYCLE (0.04f)
#define BUCK_MAX_DUTY_CYCLE   (0.95f)
#define BUCK_MINI_DUTY_CYCLE  (0.04f)

#define BOOST_MAX_DUTY_CMP_VALUE  (HRPWM_PERIOD_VALUE*BOOST_MAX_DUTY_CYCLE*2)
#define BOOST_MINI_DUTY_CMP_VALUE (HRPWM_PERIOD_VALUE*BOOST_MINI_DUTY_CYCLE)
#define BUCK_MAX_DUTY_CMP_VALUE   (HRPWM_PERIOD_VALUE*BUCK_MAX_DUTY_CYCLE*2)
#define BUCK_MINI_DUTY_CMP_VALUE  (HRPWM_PERIOD_VALUE*BUCK_MINI_DUTY_CYCLE)
#define _10PER__DUTY_CMP_VALUE  (HRPWM_PERIOD_VALUE*0.1F)
#define _85PER__DUTY_CMP_VALUE  (HRPWM_PERIOD_VALUE*0.85F)

#define PI_CTRL_INT_MAX     (BUCK_MAX_DUTY_CMP_VALUE)
#define PI_CTRL_INT_MIN     (-BUCK_MAX_DUTY_CMP_VALUE)
#define PI_CTRL_INT_INC_MAX (HRPWM_PERIOD_VALUE * 0.30F)

#define BUCK_HRPWM_CMP_MAX_DUTY_VALUE  (GET_CMP_VALUE_BY_DUTY_C((1.0f-BUCK_MAX_DUTY_CYCLE))) /*99%*/
#define BUCK_HRPWM_CMP_MINI_DUTY_VALUE (GET_CMP_VALUE_BY_DUTY_C((1.0f-BUCK_MINI_DUTY_CYCLE))) /*10%*/
#define BUCK_GET_HRPWM_CMP_VALUE(duty) (GET_CMP_VALUE_BY_DUTY_C((1.0f-duty))) /*10%*/

#define BST_HRPWM_CMP_MAX_DUTY_VALUE  (GET_CMP_VALUE_BY_DUTY_C((1.0f-BOOST_MAX_DUTY_CYCLE))) /*90%*/
#define BST_HRPWM_CMP_MINI_DUTY_VALUE (GET_CMP_VALUE_BY_DUTY_C((1.0f-BOOST_MINI_DUTY_CYCLE))) /*10%*/
#define BST_GET_HRPWM_CMP_VALUE(duty) (GET_CMP_VALUE_BY_DUTY_C((1.0f-duty))) /*10%*/

#define HRPWM_GET_DEADTIME(nS) ((nS*100u)/13u)

/* Dead time value 130pS * 100000 = 13uS */
#define HRPWM_DEADTIME_VALUE            (HRPWM_GET_DEADTIME(50u))

typedef struct usr_hrpwm_cmp_dat{
    uint32_t cmp_a_value;
    uint32_t cmp_b_value;
    uint32_t cmp_e_value;
    uint32_t cmp_f_value;

}HRPWM_CmpDat_T;

int32_t usr_boost_hrpwm_init(void);
int32_t usr_buck_hrpwm_init(void);
HRPWM_CmpDat_T hrpwm_buck_get_cmp_value(uint32_t follow_cmp_value, uint32_t period,float duty);
HRPWM_CmpDat_T hrpwm_boost_get_cmp_value(uint32_t follow_cmp_value, uint32_t period,float duty);

void hrpwm_set_cmp_values(CM_HRPWM_TypeDef *HRPWMx,HRPWM_CmpDat_T cmp_dat);
uint32_t hrpwm_set_pwm_out_freq(CM_HRPWM_TypeDef *HRPWMx, uint32_t frea_kHz);
void usr_hrpwm_start(void);
void usr_hrpwm_stop_all(void);

#ifdef __cplusplus
}
#endif

#endif //USR_HRPWM_HANDLE__H_H


